<template>
  <t-modal
    :title="operateType == 1 ? '新增' : '编辑'"
    width="900px"
    :visible="visible"
    @cancel="closeModal()"
    @ok="handelOk()"
    wrapClassName="addFilesModal-container"
    v-bind="footer"
    forceRender
  >
    <div class="curve-modal-body">
      <a-form-model ref="ruleForm" :model="form" :rules="rules">
        <a-form-model-item label="案卷题名" prop="caseTitle">
          <a-input
            v-model.trim="form.caseTitle"
            placeholder="请输入"
            type="textarea"
            :rows="4"
          />
        </a-form-model-item>
        <a-form-model-item label="档号" prop="documentCode">
          <a-input v-model.trim="form.documentCode" placeholder="请输入" />
        </a-form-model-item>
        <a-form-model-item label="立卷单位" prop="filingUnit">
          <a-input v-model.trim="form.filingUnit" placeholder="请输入" />
        </a-form-model-item>
        <a-form-model-item label="起止时间" prop="defaultTime">
          <a-range-picker
            v-model="form.defaultTime"
            :placeholder="['开始日期', '开始日期']"
            format="YYYY-MM-DD"
            valueFormat="YYYY-MM-DD"
            style="width:100%"
          >
            <a-icon slot="suffixIcon" type="calendar" />
          </a-range-picker>
        </a-form-model-item>
        <a-form-model-item label="保管期限" prop="retentionPeriod">
          <a-select v-model="form.retentionPeriod" placeholder="请选择">
            <a-select-option
              :value="item.dicItemCode"
              v-for="item in retentionSoucre"
              :key="item.dicItemCode"
            >
              {{ item.dicItemName }}
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="备注" prop="remark">
          <a-input
            v-model.trim="form.remark"
            placeholder="请输入"
            type="textarea"
            :rows="4"
          />
        </a-form-model-item>
      </a-form-model>
    </div>
  </t-modal>
</template>

<script>
import {
  saveFilesDirectory,
  detailFilesDirectory,
  updateFilesDirectory,
} from "@/api/safetyManage";
import { getDicItemByCode } from "@/api/common.js";
export default {
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    operateType: {
      type: [String, Number],
      default: "1",
    },
    parameter: {
      type: Object,
      default() {
        return {};
      },
    },
    footer: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      //保管期限
      retentionSoucre: [],
      form: {
        //案卷题名
        caseTitle: "",
        //档号
        documentCode: "",
        //立卷单位
        filingUnit: "",
        //起止时间
        defaultTime: [],
        //保管期限
        retentionPeriod: undefined,
        //备注
        remark: "",
      },
      rules: {
        caseTitle: [
          {
            required: true,
            message: "请输入案卷题名",
          },
        ],
        documentCode: [
          {
            required: true,
            message: "请输入档号",
          },
        ],
        filingUnit: [
          {
            required: true,
            message: "请输入立卷单位",
          },
        ],
        defaultTime: [
          {
            required: true,
            message: "请选择起止时间",
          },
        ],
        retentionPeriod: [
          {
            required: true,
            message: "请选择保管期限",
          },
        ],
      },
    };
  },
  watch: {
    visible: {
      handler(val) {
        this.$emit("update:visible", val);
        if (val) {
          this.getRetentionData();
          let { uuid } = this.parameter;
          if (uuid) {
            this.getDetail();
          }
        }
      },
      immediate: true,
    },
  },

  methods: {
    saveFilesDirectory,
    updateFilesDirectory,
    //查询保管期限
    getRetentionData() {
      return new Promise((resolve, reject) => {
        getDicItemByCode({ code: "retentionPeriod" })
          .then((res) => {
            let { data } = res;
            this.retentionSoucre = data ?? [];
            resolve();
          })
          .catch((_) => {
            this.retentionSoucre = [];
            reject();
          })
          .finally((_) => {});
      });
    },

    getDetail() {
      let { uuid } = this.parameter;
      return new Promise((resolve, reject) => {
        detailFilesDirectory({ uuid })
          .then((res) => {
            let { data } = res;
            this.form = {
              //案卷题名
              caseTitle: data.caseTitle,
              //档号
              documentCode: data.documentCode,
              //立卷单位
              filingUnit: data.filingUnit,
              //起止时间
              defaultTime: [data.startDate, data.endDate],
              //保管期限
              retentionPeriod: data.retentionPeriod,
              //备注
              remark: data.remark,
            };
            resolve();
          })
          .catch((_) => {
            reject();
          })
          .finally((_) => {});
      });
    },
    handelOk() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          let { form } = this;
          let { proCode, classifyId, uuid } = this.parameter;
          let formJson = {
            //案卷题名
            caseTitle: form.caseTitle,
            //档号
            documentCode: form.documentCode,
            //立卷单位
            filingUnit: form.filingUnit,
            //起止时间
            startDate: form.defaultTime[0],
            endDate: form.defaultTime[1],
            //保管期限
            retentionPeriod: form.retentionPeriod,
            //备注
            remark: form.remark,
          };
          let url = "";
          //编辑
          if (uuid) {
            formJson.uuid = uuid;
            url = "updateFilesDirectory";
          } else {
            formJson.proCode = proCode;
            formJson.classifyId = classifyId;
            url = "saveFilesDirectory";
          }
          this[url](formJson)
            .then((res) => {
              if (res.statusCode === 200 || res === 1) {
                this.$message.success("操作成功", 1.5);
                this.closeModal();
                this.$emit("ok");
              } else {
                this.$message.error(res?.message ?? "操作失败");
              }
            })
            .catch((_) => {});
        }
      });
    },
    //重置表单
    resetForm() {
      this.form = {
        //案卷题名
        caseTitle: "",
        //档号
        documentCode: "",
        //立卷单位
        filingUnit: "",
        //起止时间
        defaultTime: [],
        //保管期限
        retentionPeriod: undefined,
        //备注
        remark: "",
      };
      this.$refs.ruleForm.resetFields();
    },
    //关闭
    closeModal() {
      this.resetForm();
      this.$emit("update:visible", false);
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.addFilesModal-container {
  .curve-modal-body {
    max-height: 700px;
    overflow: auto;
  }
  .ant-form-item {
    display: flex;
    .ant-form-item-label {
      flex-shrink: 0;
      width: 100px;
    }
    .ant-form-item-control-wrapper {
      flex: 1;
    }
  }
}
</style>
